/*
 * Copyright (c) 2012 Andrea De Rinaldis <ozioso at ipignoli.dyndns.org>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.dyndns.ipignoli.droidcruise.themes;

import java.util.HashMap;
import java.util.Map;
import android.widget.ImageView;


public abstract class Theme{
  public static final int LEVEL_NULL = 0, LEVEL_OK = 1, LEVEL_WARNING = 2,
      LEVEL_ALERT = 3;

  protected static class ThemeItem{
    private String  key;
    private Integer value;

    protected ThemeItem(String key, Integer value){
      this.key = key;
      this.value = value;
    }
  }

  private Map<String, Integer>  images, images_ok, images_warning,
      images_alert;
  private Map<Integer, Integer> imageCache;

  public Theme(ThemeItem[] images, ThemeItem[] images_ok,
      ThemeItem[] images_warning, ThemeItem[] images_alert){
    this.images = loadMap(images);
    this.images_ok = loadMap(images_ok);
    this.images_warning = loadMap(images_warning);
    this.images_alert = loadMap(images_alert);
    imageCache = new HashMap<Integer, Integer>();
  }

  public int getValue(String key){
    return getValue(key, LEVEL_NULL);
  }

  public int getValue(String key, int level){
    Map<String, Integer> m = images;
    switch(level){
      case LEVEL_OK:
        m = images_ok;
        break;
      case LEVEL_WARNING:
        m = images_warning;
        break;
      case LEVEL_ALERT:
        m = images_alert;
        break;
    }
    if(m.containsKey(key))
      return m.get(key).intValue();
    return images.get(key).intValue();
  }

  public void setValue(ImageView[] view, long value, int level){
    if(value < 0){
      setImage(view[2], getValue("-", level));
      setImage(view[1], getValue("-", level));
      setImage(view[0], getValue("-", level));
      return;
    }

    if(value >= 1000){
      setImage(view[2], getValue("m", level));
      setImage(view[1], getValue("a", level));
      setImage(view[0], getValue("x", level));
      return;
    }

    int set;
    for(int i = 0; i < view.length; i++, value /= 10){
      set = (int)value % 10;
      setImage(
          view[i],
          getValue(
              i > 0 && set == 0 && set == value ? "empty" : String.valueOf(set),
              level));
    }
  }

  private Map<String, Integer> loadMap(ThemeItem[] items){
    Map<String, Integer> ret = new HashMap<String, Integer>();
    for(ThemeItem t: items)
      ret.put(t.key, t.value);
    return ret;
  }

  private void setImage(ImageView view, int resource){
    Integer viewId = new Integer(view.getId());
    if(imageCache.containsKey(viewId)
        && imageCache.get(viewId).intValue() == resource)
      return;
    view.setImageResource(resource);
    imageCache.put(viewId, new Integer(resource));
  }
}
